<?php 
/**
 * jifen
 * 
 */
require_once(dirname(__FILE__)."/config.php");
require_once(DEDEINC."/datalistcp.class.php");
CheckRank(0,0);
$menutype = 'shouye';
setcookie("ENV_GOBACK_URL",GetCurUrl(),time()+3600,"/");
if($dopost=='')
{
	$sql = "select * from `#@__jifen`  WHERE mid='".$cfg_ml->M_ID."' order by addtime desc ";
    $dlist = new DataListCP();
    $dlist->pageSize =14;
    $dlist->SetTemplate(DEDEMEMBER."/templets/jf.htm");    
    $dlist->SetSource($sql);
    $dlist->Display(); 
    exit();
}
else
{
    if($cfg_money_scores==0)
    {
        $arr = array(
	       "zhi"=>"err",
	        "msg"=>"系统禁用了积分与金币兑换功能！"
        );
        echo json_encode($arr);
        exit;
    }
    $money = empty($money) ? "" : abs(intval($money));
    if(empty($money))
    {
        $arr = array(
	       "zhi"=>"err",
	        "msg"=>"您没指定要兑换多少金币！"
        );
        echo json_encode($arr);
        exit;
    }
        
    $needscores = $money * $cfg_money_scores;
    if($cfg_ml->fields['scores'] < $needscores )
    {
        $arr = array(
	       "zhi"=>"err",
	        "msg"=>"您积分不足，不能换取这么多的金币！"
        );
        echo json_encode($arr);
        exit;
    }
    $litmitscores = $cfg_ml->fields['scores'] - $needscores;
        
    //保存记录
    $mtime = time();
    $inquery = "INSERT INTO `#@__member_operation`(`buyid` , `pname` , `product` , `money` , `mtime` , `pid` , `mid` , `sta` ,`oldinfo`)
        VALUES ('ScoresToMoney', '积分换金币操作', 'stc' , '0' , '$mtime' , '0' , '{$cfg_ml->M_ID}' , '0' , '用 {$needscores} 积分兑了换金币：{$money} 个'); ";
    $dsql->ExecuteNoneQuery($inquery);
	//保存表
	$addtime = time();
    $inquery1 = "INSERT INTO `#@__jifen`(`mn` , `addtime` , `mid` , `jf`)
        VALUES ('{$money}' , '$addtime' , '{$cfg_ml->M_ID}' , '$needscores'); ";
    $dsql->ExecuteNoneQuery($inquery1);
    //修改积分与金币值
    $dsql->ExecuteNoneQuery("UPDATE `#@__member` SET `scores`=$litmitscores, money= money + $money  WHERE mid='".$cfg_ml->M_ID."' ");
        
    // 清除会员缓存
    $cfg_ml->DelCache($cfg_ml->M_ID);
    
    $arr = array(
	       "zhi"=>"ok",
	        "msg"=>"恭喜您，成功用 {$needscores} 积分总换了{$money}元"
        );
        echo json_encode($arr);
        exit;
}